package com.bj58.groupbuy.action.util;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

/**
 * 通过oppenOffice将office文档转换为pdf
 * 对含有图片的ppt转换效果较差
 * @author zhaoxiang
 *
 */
public class Office2PDF {
	private static String officeHome;
	static{
		officeHome = getOfficeHome();
	}
	
	  /** jodconverter3.0
	 * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice 
	 *  
	 * @param sourceFile 
	 *            源文件,绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等. 
	 *  
	 * @param destFile 
	 *            目标文件.绝对路径. 
	 * @throws Exception 
	 */  
	public static boolean office2pdf_V2(String inputFilePath) throws Exception {
	    DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  
	    config.setOfficeHome(officeHome);  
	    OfficeManager officeManager = config.buildOfficeManager();  
	   try{
	    officeManager.start();
	    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
	    String outputFilePath = getOutputFilePath(inputFilePath);
	    File inputFile = new File(inputFilePath);
	    if (inputFile.exists()) {// 找不到源文件, 则返回  
	        File outputFile = new File(outputFilePath).getCanonicalFile();  
	        if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径  
	            outputFile.getParentFile().mkdirs();  
	        }  
	        converter.convert(inputFile,outputFile);
	    }  
	   }catch(Exception e){
		   e.printStackTrace();
			StringWriter writer=new StringWriter();
			e.printStackTrace(new PrintWriter(writer));
			writer.close();
		   throw new Exception(writer.toString());
	   }finally{
	    	if(officeManager != null )
	    		officeManager.stop();  
	    }
	    return true;
	}  
	  
	
	public static String getOutputFilePath(String inputFilePath) {
		int index = inputFilePath.lastIndexOf(".");
	    String outputFilePath = inputFilePath.substring(0,index)+".pdf";  
	    return outputFilePath;  
	}  
	
	public static String getOfficeHome() {  
	    String osName = System.getProperty("os.name");  
	    if (Pattern.matches("Linux.*", osName)) {  
	        return "/opt/openoffice4";  
	    } else if (Pattern.matches("Windows.*", osName)) {  
	    	return "D:/openoffice";  
//	    	return "D:/libreOffice4";  
	    } else if (Pattern.matches("Mac.*", osName)) {  
	        return "/Application/OpenOffice.org.app/Contents";  
	    }  
	    return null;  
	}  
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
//		office2PDF("docs/2014.doc","docs/2014.pdf");
		try {
			office2pdf_V2("docs/2014.doc");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
